package com.chaos.eurekaproducer.leetCode;

import com.chaos.eurekaproducer.basic.ListNode;

import java.util.List;

/**
 * @author liaopeng
 * @title: Offer24
 * @projectName eureka-producer
 * @description:
 * 定义一个函数，输入一个链表的头节点，反转该链表并输出反转后链表的头节点。
 *
 *  
 *
 * 示例:
 *
 * 输入: 1->2->3->4->5->NULL
 * 输出: 5->4->3->2->1->NULL
 *
 *
 * @date 2021/6/10下午4:34
 */
public class Offer24 {
    public static ListNode reverseList(ListNode head) {
        if (head==null || head.getNext()==null){
            return head;
        }
        ListNode preNode = head;
        ListNode curNode = preNode.getNext();
        ListNode nextNode =null;
        head.setNext(null);
        while (curNode!=null){
            nextNode = curNode.getNext();
            curNode.setNext(preNode);
            preNode=curNode;
            curNode = nextNode;
        }
        return preNode;
    }

    public static void main(String[] args) {
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = new ListNode(2);
        ListNode listNode3 = new ListNode(3);
        ListNode listNode4 = new ListNode(4);
        ListNode listNode5 = new ListNode(5);
        listNode4.setNext(listNode5);
        listNode3.setNext(listNode4);
        listNode2.setNext(listNode3);
        listNode1.setNext(listNode2);
        ListNode reverseList = reverseList(listNode1);
        System.out.println(reverseList.getVal());
    }
}
